package com.opgea.crm.repository;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.opgea.crm.common.constant.ApplicationConstant;
import com.opgea.crm.domain.entities.Employee;
import com.opgea.crm.domain.entities.ProjectDetails;
import com.opgea.crm.domain.entities.ProjectDocuments;

@Repository
public  class ProjectDetailsRepoImpl implements ProjectDetailsRepo{
	
	@PersistenceContext
	private EntityManager em;

	@Override
	public void create(ProjectDetails projectDetails) {
		em.persist(projectDetails);
	}

	@Override
	public ProjectDetails find(Long id) {
		return em.find(ProjectDetails.class, id);
	}

	@Override
	public List<ProjectDetails> findAll() {
		String query = "SELECT p FROM ProjectDetails p";
		return em.createQuery(query, ProjectDetails.class).getResultList();
	}
	
	@Override
	public List<ProjectDetails> findAllByCompanyId(Long id) {
		String query = "SELECT p FROM ProjectDetails p WHERE p.customer.company.id = "+id;
		return em.createQuery(query, ProjectDetails.class).getResultList();
	}

	
	public void remove(Long id) {
		
		
	}

	
	public void update(ProjectDetails projectDetails)
	{
		if (em.find(ProjectDetails.class, projectDetails.getId()) == null) {
	        throw new IllegalArgumentException("Unknown ProjectId id: " + projectDetails.getId());
	      }
		ProjectDetails managedEmployee = em.merge(projectDetails);
		
	}

	@Override
	public List<Employee> findMemberList(String criteria,long projectId) {
		StringBuffer buffer=new StringBuffer("select e.* from employee e " +
											"where e.id not in(select pe.members_id from projectdetails_employee pe" +
											" where pe.workinginprojects_id='"+projectId+"'  group by pe.members_id)");
		String cond=" AND ";
		
		if( (Integer.valueOf(criteria) == ApplicationConstant.EmployeeQualifier.CLIENT.ordinal())
				){
			buffer.append(cond);
			buffer.append(" e.employeeType = "+ ApplicationConstant.EmployeeQualifier.CLIENT.ordinal());
			cond = " AND ";
			}
		
		if( (Integer.valueOf(criteria) == ApplicationConstant.EmployeeQualifier.INTERNAL.ordinal())
		){
			buffer.append(cond);
			buffer.append(" e.employeeType ="+ApplicationConstant.EmployeeQualifier.INTERNAL.ordinal());
			cond = " AND ";
		}
		
		if( (Integer.valueOf(criteria) == ApplicationConstant.EmployeeQualifier.OTHERS.ordinal())
		){
			buffer.append(cond);
			buffer.append(" e.employeeType  ="+ApplicationConstant.EmployeeQualifier.OTHERS.ordinal());
		}
		
		Query query=em.createNativeQuery(buffer.toString(),Employee.class);
		return (List<Employee>)query.getResultList();
	}

	@Override
	public List<Employee> findByCompanyId(long companyId) {
		String query = "SELECT e FROM Employee e WHERE e.company.id = "+companyId;
		return em.createQuery(query,Employee.class).getResultList();
		
	}
	
	@Override
	public List<ProjectDetails> findProjectList(String criteria, String value) {
		
		StringBuffer buffer=new StringBuffer("SELECT p FROM  ProjectDetails p");
		String cond=" WHERE ";
		
		System.out.println("Criteria : "+criteria);
		
		if( (Integer.valueOf(criteria) == ApplicationConstant.ProjectSearchCriteriaQualifier.ProjectName.ordinal()))
		{
			buffer.append(cond);
			buffer.append(" p.projectName like :paramVal");
			
		}
		
		if( (Integer.valueOf(criteria) == ApplicationConstant.ProjectSearchCriteriaQualifier.Priority.ordinal()))
		{
			buffer.append(cond);
			buffer.append(" p.priority.fieldValue like :paramVal");
			
		}
		
		Query query=em.createQuery(buffer.toString());
		query.setParameter("paramVal", value+'%');
		
		return (List<ProjectDetails>)query.getResultList();
	}

	@Override
	public List<ProjectDetails> findProjectReportDetails(long projectId) {
		String query = "SELECT p FROM ProjectDetails p";
		return em.createQuery(query,ProjectDetails.class).getResultList();
	}

	
	//for upload document in project module
	@Override
	public void createProjectDoc(ProjectDocuments documents){
	    	em.persist(documents);
	}

	public List<ProjectDocuments> findAllByProject(long projectId){
		String query = "SELECT pd FROM ProjectDocuments pd WHERE pd.project.id = "+projectId;
		List<ProjectDocuments> projectDocList = em.createQuery(query, ProjectDocuments.class).getResultList();
		return projectDocList;
	}
	
	
	
}